# Replication materials ---------------------------------------------------
# Paper:    Campaigns and the Selection of Policy-seeking Representatives.
#           Legislative Studies Quarterly
# Authors: Shaun Bowler, Gail McElroy, Stefan Müller


#--------------------------------------------------------------------------
# Script for creating master data (Dail Eireann 30, 2007-2011)
#--------------------------------------------------------------------------

# Set the working directory

# Load necessary packages -------------------------------------------------

library(foreign)
library(Hmisc)
library(tidyverse)

# Import original files into R environment -----------------------------

# Import the CCS 2012 survey (30th Dáil)
# We manually added the pid column to merge the files afterwards and removed the labels in Stata

# NOTE: THIS FILE CANNOT BE INCLUDED IN THE REPLICATION MATERIAL
# IF YOU HAVE ACCESS TO THE DATA AND REQUIRE THE PID VARIABLE, PLEASE CONTACT THE AUTHORS OF THIS PAPER
CCS30 <- read.dta("data_ireland_2007-2011/CCS_Ireland_2007.dta") 

CCS30 <- CCS30 %>% 
  mutate(partyCCS = car::recode(a1, "1='Fianna Fail'; 2='Fine Gael'; 
                                3='Greens'; 4='Labour Party'; 5='PD'; 6='Sinn Fein'; 7='Independent'")) %>% 
  mutate(ageCCS = 2007 - e2) %>% 
  mutate(genderCCS = car::recode(e1, "1='male'; 2='female'")) %>% 
  mutate(incumbentPrevCCS = as.factor(car::recode(a6c, "NA='no';0='no';else='yes'")))

# Create dummy variably "include" to check the difference in activity between people who 
# participated in survey and those who did not.
CCS30$respondent <- factor("1")

# Create campaign index ---------------------------------------------------

# Classic campaigning
CCS30$use_callingvoters <- car::recode(CCS30$b5aa, "2:5=1; 1=0")
CCS30$use_directmail <- car::recode(CCS30$b5ab, "2:5=1; 1=0")
CCS30$use_debating <- car::recode(CCS30$b5ac, "2:5=1; 1=0")
CCS30$use_internet <- car::recode(CCS30$b5ad, "2:5=1; 1=0")
CCS30$use_partymembers <- car::recode(CCS30$b5ae, "2:5=1; 1=0")
CCS30$use_partyofficials <- car::recode(CCS30$b5af, "2:5=1; 1=0")
CCS30$use_nateventsattend <- car::recode(CCS30$b5ag, "2:21=1; 1=0")
CCS30$use_localeventsattend <- car::recode(CCS30$b5ah, "2:5=1; 1=0")
CCS30$use_fundraisingevents <- car::recode(CCS30$b5ai, "2:5=1; 1=0")
CCS30$use_appointmentscompanies <- car::recode(CCS30$b5ba, "2:5=1; 1=0")
CCS30$use_appointmentsclubs <- car::recode(CCS30$b5bb, "2:5=1; 1=0")
CCS30$use_organiserallies <- car::recode(CCS30$b5bc, "2:5=1; 1=0")
CCS30$use_localnewspaper <- car::recode(CCS30$b5bd, "2:5=1; 1=0")
CCS30$use_natnewspaper <- car::recode(CCS30$b5be, "2:5=1; 1=0")
CCS30$use_localtvradio <- car::recode(CCS30$b5bf, "2:5=1; 1=0")
CCS30$use_nattvradio <- car::recode(CCS30$b5bg, "2:5=1; 1=0")


# Promotion material 
CCS30$use_posters <- car::recode(CCS30$b6aa, "2=1; 1=0")
CCS30$use_billboards <- car::recode(CCS30$b6ab, "2=1; 1=0")
CCS30$use_adsinnews <- car::recode(CCS30$b6ac, "2=1; 1=0")
CCS30$use_officehours <- car::recode(CCS30$b6ad, "2=1; 1=0")
CCS30$use_socialgatherings <- car::recode(CCS30$b6ae, "2=1; 1=0")
CCS30$use_flyers <- car::recode(CCS30$b6af, "2=1; 1=0")

# Post-modern campaigning
CCS30$use_website <- car::recode(CCS30$b6ag, "2=1; 1=0")
CCS30$use_emaillist <- car::recode(CCS30$b9c, "2=1; 1=0")
CCS30$use_campaignvideo <- car::recode(CCS30$b9d, "2=1; 1=0")
CCS30$use_onlinechats <- car::recode(CCS30$b9e, "2=1; 1=0")
# b9f: I advertised my webpage and/or email address on campaign literature
CCS30$use_blog <- car::recode(CCS30$b9g, "2=1; 1=0")

# Create additive indices

CCS30$use_classic <- rowSums(CCS30[,c("use_callingvoters",
                                      "use_directmail",
                                      "use_debating",
                                      "use_partymembers",
                                      "use_partyofficials",
                                      "use_nateventsattend",
                                      "use_localeventsattend",
                                      "use_fundraisingevents",
                                      "use_appointmentscompanies",
                                      "use_appointmentsclubs",
                                      "use_localnewspaper",
                                      "use_natnewspaper",
                                      "use_localtvradio",
                                      "use_nattvradio")], na.rm=TRUE)

CCS30$use_material <- rowSums(CCS30[,c("use_posters",
                                       "use_billboards",
                                       "use_adsinnews",
                                       "use_officehours",
                                       "use_socialgatherings",
                                       "use_flyers")], na.rm=TRUE)

CCS30$use_postmodern <- rowSums(CCS30[,c("use_website",
                                       "use_emaillist",
                                       "use_campaignvideo",
                                       "use_onlinechats",
                                       "use_blog")], na.rm=TRUE)

CCS30$use_campaign <- rowSums(CCS30[,c("use_classic",
                                       "use_material",
                                       "use_postmodern")], na.rm = TRUE)



# Recode variables for ordinal index --------------------------------------

CCS30$use_ord_callingvoters <- car::recode(CCS30$b5aa, "1=0")
CCS30$use_ord_directmail <- car::recode(CCS30$b5ab, "1=0")
CCS30$use_ord_debating <- car::recode(CCS30$b5ac, "1=0")
CCS30$use_ord_internet <- car::recode(CCS30$b5ad, "1=0")
CCS30$use_ord_partymembers <- car::recode(CCS30$b5ae, "1=0")
CCS30$use_ord_partyofficials <- car::recode(CCS30$b5af, "1=0")
CCS30$use_ord_nateventsattend <- car::recode(CCS30$b5ag, "1=0")
CCS30$use_ord_localeventsattend <- car::recode(CCS30$b5ah, "1=0")
CCS30$use_ord_fundraisingevents <- car::recode(CCS30$b5ai, "1=0")
CCS30$use_ord_appointmentscompanies <- car::recode(CCS30$b5ba, "1=0")
CCS30$use_ord_appointmentsclubs <- car::recode(CCS30$b5bb, "1=0")
CCS30$use_ord_organiserallies <- car::recode(CCS30$b5bc, "1=0")
CCS30$use_ord_localnewspaper <- car::recode(CCS30$b5bd, "1=0")
CCS30$use_ord_natnewspaper <- car::recode(CCS30$b5be, "1=0")
CCS30$use_ord_localtvradio <- car::recode(CCS30$b5bf, "1=0")
CCS30$use_ord_nattvradio <- car::recode(CCS30$b5bg, "1=0")

# Create additive ordinal index

CCS30$use_ord_total <- rowSums(CCS30[,c("use_ord_callingvoters",
                                      "use_ord_directmail",
                                      "use_ord_debating",
                                      "use_ord_internet",
                                      "use_ord_partymembers",
                                      "use_ord_partyofficials",
                                      "use_ord_nateventsattend",
                                      "use_ord_localeventsattend",
                                      "use_ord_fundraisingevents",
                                      "use_ord_appointmentscompanies",
                                      "use_ord_organiserallies",
                                      "use_ord_appointmentsclubs",
                                      "use_ord_localnewspaper",
                                      "use_ord_natnewspaper",
                                      "use_ord_localtvradio",
                                      "use_ord_nattvradio")], na.rm=TRUE)


# New factor variable with party affiliation for all respondents
CCS30$party_factor <- as.factor(CCS30$a1)

# Change "respondent" variable (NA to 0)
CCS30$respondent <- "1" 

CCS30 <- CCS30 %>% 
  mutate(pid = ifelse(pid == ".", NA, pid)) %>% 
  filter(!is.na(pid))

# Merge datasets ----------------------------------------------------------

# Import the activity file 
Dail30 <- read.csv("data_ireland_2007-2011/dail_30_activity.csv")

Dail30candidates <- read.csv("data_ireland_2007-2011/dail_30_candidates.csv") %>% 
  mutate(pid = ifelse(pid == "", NA, as.character(pid)))   # recode PID, otherwise merging fails

Dail30candidates_activity <- left_join(Dail30candidates, Dail30, by = "pid")

Dail30candidates_activity_survey <- dplyr::left_join(Dail30candidates_activity, CCS30, by = "pid") 

Dail30parties <- read.csv("data_ireland_2007-2011/dail_30_parties.csv")
Dail30_Survey <- dplyr::left_join(Dail30candidates_activity_survey, Dail30parties, by = "party")

#Dail30_Survey_only_survey <- dplyr::left_join(Dail30_Survey_parties, Dail30candidates,  by = "pid")
Dail30_Survey$respondent <- car::recode(Dail30_Survey$respondent, "1=1; NA=0")

# Rename speeches_words_total
Dail30_Survey$words_total <- Dail30_Survey$speeches_words_total

# Recode minister variable
Dail30_Survey$minister_numeric <- as.numeric(Dail30_Survey$minister_sum)
Dail30_Survey$minister <- car::recode(Dail30_Survey$minister_numeric, "1:3=1; 'NA'=0")

# New variable for spending (divide by 1000)
Dail30_Survey$campaign_spending1000 <- (Dail30_Survey$campaign_spending) / 1000

# Recode gender
Dail30_Survey$female <- car::recode(Dail30_Survey$gender, "'F'=1; 'M'=0")
Dail30_Survey$female_elected <- car::recode(Dail30_Survey$gender_elected, "'F'=1; 'M'=0")

# Calculate age and age squared
Dail30_Survey$age <- 2007 - Dail30_Survey$year_of_birth

Dail30_Survey$age_squared <- (Dail30_Survey$age)^2


Dail30_Survey$wonseat <- ifelse(!is.na(Dail30_Survey$pid), 1, 0)

# Keep variables needed for regressions ---

Dail30_Survey_subset <- dplyr::select(Dail30_Survey,
                               respondent,
                               won,
                               wonseat, 
                               partyCCS,
                               ageCCS,
                               genderCCS,
                               Gender,
                               incumbentPrevCCS,
                               incumbent,
                               speeches_total,
                               committee_chair,
                               opposition_leader,
                               female,
                               party,
                               questions_total,
                               speeches_long_total,
                               speeches_words_total, 
                               words_total,
                               use_callingvoters,
                               use_directmail,
                               use_debating,
                               use_partymembers,
                               use_partyofficials,
                               use_nateventsattend,
                               use_localeventsattend,
                               use_fundraisingevents,
                               use_appointmentscompanies,
                               use_appointmentsclubs,
                               use_localnewspaper,
                               use_natnewspaper,
                               use_localtvradio,
                               use_nattvradio,
                               use_posters,
                               use_billboards,
                               use_adsinnews,
                               use_officehours,
                               use_socialgatherings,
                               use_flyers,
                               use_website,
                               use_emaillist,
                               use_campaignvideo,
                               use_onlinechats,
                               use_blog,
                               use_classic,
                               use_campaign,
                               use_material,
                               use_postmodern,
                               use_ord_callingvoters,
                               use_ord_directmail,
                               use_ord_debating,
                               use_ord_internet,
                               use_ord_partymembers,
                               use_ord_partyofficials,
                               use_ord_nateventsattend,
                               use_ord_localeventsattend,
                               use_ord_fundraisingevents,
                               use_ord_appointmentscompanies,
                               use_ord_organiserallies,
                               use_ord_appointmentsclubs,
                               use_ord_localnewspaper,
                               use_ord_natnewspaper,
                               use_ord_localtvradio,
                               use_ord_nattvradio,
                               use_ord_total,
                               party_factor,
                               respondent,
                               party,
                               minister,
                               campaign_spending1000,
                               female_elected,
                               age, 
                               age_squared,
                               seat_share_ind0)
                               
# Save this object as a csv file which can be used to replicate all regressions ---

dail_30_final <- Dail30_Survey_subset %>%  
  mutate(words_total_1000 = words_total/1000) %>% 
  mutate(government = car::recode(party, "'Fianna Fail'='Government'; 'Green Party'='Government'; else='Opposition'")) %>% 
  mutate(country_id = "Ireland")

write_csv(dail_30_final, "Dail30_final.csv")

